package com.example.day07;

public class Book {

    private   int id;
    private  String bookName;
    private  double  price;
    private  BookInfo bookInfo;

    public Book() {
    }

    public Book(int id, String bookName, double price) {
        this.id = id;
        this.bookName = bookName;
        this.price = price;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getBookName() {
        return bookName;
    }

    public void setBookName(String bookName) {
        this.bookName = bookName;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public BookInfo getBookInfo() {
        return bookInfo;
    }

    public void setBookInfo(BookInfo bookInfo) {
        this.bookInfo = bookInfo;
    }
    //重写方法
    //@Override
    //public boolean equals(Object obj){
    //    //b1.equals(null);
    //    //1. 非空性
    //    if(obj == null)
    //        return false;
    //
    //    //b1.equals(b1);
    //    //2. 自反性
    //    if(this == obj)
    //        return true;
    //
    //    //自定义比较哪些属性
    //    //某些属性一致的时候,期望equals比较返回true
    //    //3. 类型一致性的判断
    //    //b1.equals(s1);
    //    if(obj instanceof Book){
    //        //通常情况是使用id或者编号
    //        //id如果一样,就希望返回true
    //        Book b = (Book) obj;
    //        //return id == b.getId();
    //
    //        //id和图书名称一致,返回true
    //        //java.lang.String => 重写了equals
    //        //return id==b.getId() && bookName.equals(b.bookName);
    //
    //        //id和bookInfo中的id
    //        return id==b.getId() && bookInfo.equals(b.bookInfo);
    //    }
    //    //if(getClass()==obj.getClass()){
    //    //
    //    //}
    //    return false;
    //}
//    @Override
//    public boolean equals(Object obj) {
//         if(obj==null){
//             return  false;
//         }
//
//         if(this==obj){
//             return  true;
//         }
//         if(obj  instanceof  Book){
////             return  true;
//
//             //判断图书名和id是否一样
//             Book b= (Book) obj;
//
//             return id==b.getId()&&bookName.equals(((Book) obj).getBookName());
//         }
//
//         if(getClass()==obj.getClass()){
//             return  true;
//         }
//
//         return  false;
//    }

    @SuppressWarnings("all")
    @Override
    public boolean equals(Object o) {
        System.out.println("equals...");
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Book book = (Book) o;

        if (id != book.id) return false;
        return bookName != null ? bookName.equals(book.bookName) : book.bookName == null;
    }

    @Override
    public int hashCode() {
        int result = id;
        result = 31 * result + (bookName != null ? bookName.hashCode() : 0);
        System.out.println("hashCode:"+result);
        return result;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        //重新去new一个对象
        Book book=new Book();

        book.setId(id);
        book.setBookName(bookName);
        book.setPrice(price);

        BookInfo Info=bookInfo;

        if(null!=Info){
            BookInfo newinfo=new BookInfo();
            newinfo.setId(Info.getId());
            newinfo.setAuthor(Info.getAuthor());
            book.setBookInfo(newinfo);
        }

        return  book;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("Book{");
        sb.append("id=").append(id);
        sb.append(", bookName='").append(bookName).append('\'');
        sb.append(", price=").append(price);
        sb.append('}');
        return sb.toString();
    }
}
